package com.iotimc.networklibrary.core;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.IOException;
import java.io.Reader;

/**
 *@author  david
 */
public class NetUtils {
	/**
	 * 检查网络状态
	 * @param ctx
	 * @return
	 */
	public static boolean isNetworkAvailable(Context ctx) {
	    ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
	    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
	}

	public static String readTextFromReader(Reader reader) {
		StringBuffer sb = new StringBuffer();
		char[] buf = new char[4096];

		try {
			int readLen;
			try {
				while(-1 != (readLen = reader.read(buf))) {
					sb.append(buf, 0, readLen);
				}
			} catch (IOException var13) {
				var13.printStackTrace();
			}
		} finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException var12) {
					var12.printStackTrace();
				}
			}

		}

		return sb.toString();
	}
}
